package com.ruoyi.web.service.impl;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.DataTypeEnum;
import com.ruoyi.common.enums.ReportStatusEnum;
import com.ruoyi.common.enums.TrainTypeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.RailwayBigmodelVo;
import com.ruoyi.web.domain.RailwayBigmodel;
import com.ruoyi.web.mapper.RailwayBigmodelMapper;
import com.ruoyi.web.service.IRailwayBigmodelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * tlga大模型Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-13
 */
@Service
public class RailwayBigmodelServiceImpl implements IRailwayBigmodelService {

    @Resource
    private RailwayBigmodelMapper railwayBigmodelMapper;

//    @Value("${word.describe.yfjsba}")
//    private String yfjsbaDescribe;
//
//    @Value("${word.describe.sxyyba}")
//    private String sxyybaDescribe;

    /**
     * 查询tlga大模型
     *
     * @param id tlga大模型主键
     * @return tlga大模型
     */
    @Override
    public RailwayBigmodel selectRailwayBigmodelByID(String id) {
        RailwayBigmodel railwayBigmodel = railwayBigmodelMapper.selectRailwayBigmodelByID(id);
        if (railwayBigmodel != null && StringUtils.isNotEmpty(railwayBigmodel.getFilePath())){
            String[] split = railwayBigmodel.getFilePath().split("/");
            railwayBigmodel.setFileName(split.length > 0 ? split[split.length - 1] : null);
        }
        if (railwayBigmodel != null){
            railwayBigmodel.setMobile(railwayBigmodel.getTelephone());
            railwayBigmodel.setUser(railwayBigmodel.getCreateBy());
            railwayBigmodel.setTrainTypeString(TrainTypeEnum.toTrainTypeString(railwayBigmodel.getTrainType()));
        }
        return railwayBigmodel;
    }

    /**
     * 查询tlga大模型列表
     *
     * @param railwayBigmodelVo tlga大模型
     * @return tlga大模型
     */
    @Override
    public List<RailwayBigmodel> selectRailwayBigmodelList(RailwayBigmodelVo railwayBigmodelVo) {
        Integer pageSize = railwayBigmodelVo.getPageSize();
        Integer pageNum = railwayBigmodelVo.getPageNum();
        railwayBigmodelVo.setPageNum((pageNum-1)*pageSize+1);
        railwayBigmodelVo.setPageSize(pageNum*pageSize);
        railwayBigmodelVo.setSecurityOrg(railwayBigmodelVo.getUser().getQuerySecurityOrg());
        return railwayBigmodelMapper.selectRailwayBigmodelList(railwayBigmodelVo);
    }

    /**
     * 新增tlga大模型
     *
     * @param railwayBigmodel tlga大模型
     * @return 结果
     */
    @Override
    public AjaxResult insertRailwayBigmodel(RailwayBigmodel railwayBigmodel) {
        String id = String.valueOf(UUID.randomUUID());
        railwayBigmodel.setId(id);
        railwayBigmodel.setReportStatus("0");
        if(StringUtils.isNotEmpty(railwayBigmodel.getComputePower()) && railwayBigmodel.getComputePower().equals("2")) {
            String remark = Optional.ofNullable(railwayBigmodel.getRemark()).orElse("");
            railwayBigmodel.setRemark(remark);
        }
        railwayBigmodel.setCreateBy(railwayBigmodel.getUser());
        railwayBigmodel.setTelephone(railwayBigmodel.getMobile());
        railwayBigmodel.setCreateTime(DateUtils.getNowDate());
        railwayBigmodel.setUpdateTime(DateUtils.getNowDate());
        int rows = railwayBigmodelMapper.insertRailwayBigmodel(railwayBigmodel);
        if(rows > 0){
            Map<String, String> result = new HashMap<>();
            result.put("id", id);
            return AjaxResult.success(result);
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改tlga大模型
     *
     * @param railwayBigmodel tlga大模型
     * @return 结果
     */
    @Override
    public int updateRailwayBigmodel(RailwayBigmodel railwayBigmodel) {
        if(StringUtils.isNotEmpty(railwayBigmodel.getComputePower()) && railwayBigmodel.getComputePower().equals("2")) {
            String remark = Optional.ofNullable(railwayBigmodel.getRemark()).orElse("");
            railwayBigmodel.setRemark(remark);
        }
        railwayBigmodel.setCreateBy(railwayBigmodel.getUser());
        railwayBigmodel.setTelephone(railwayBigmodel.getMobile());
        railwayBigmodel.setUpdateTime(DateUtils.getNowDate());
        return railwayBigmodelMapper.updateRailwayBigmodel(railwayBigmodel);
    }

    /**
     * 批量删除tlga大模型
     *
     * @param IDs 需要删除的tlga大模型主键
     * @return 结果
     */
    @Override
    public int deleteRailwayBigmodelByIDs(Long[] IDs) {
        return railwayBigmodelMapper.deleteRailwayBigmodelByIDs(IDs);
    }

    /**
     * 删除tlga大模型信息
     *
     * @param id tlga大模型主键
     * @return 结果
     */
    @Override
    public int deleteRailwayBigmodelByID(String id) {
        return railwayBigmodelMapper.deleteRailwayBigmodelByID(id);
    }

    @Override
    public Map<String, Object> selectExportDataMap(RailwayBigmodel railwayBigmodel) {
        RailwayBigmodel railwayBigmodelVO = railwayBigmodelMapper.selectRailwayBigmodelByID(railwayBigmodel.getId());
        Map<String, Object> dataMap = new HashMap<>();

        if (railwayBigmodelVO == null){
            return dataMap;
        }

        /**
         * 如果datamap内字段存在null ftl文件将会解析失败 导致word文档内存为xml编码
         * 与用户录入信息保持一致 如果没有设置此参数
         * 默认值为空字符 在word中展示为 枚举类型全部未选中 文本类型为空
         */
        dataMap.put("securityOrgName", railwayBigmodelVO.getSecurityOrgName() == null ? addSpace("",22) : addSpace(railwayBigmodelVO.getSecurityOrgName(),22));
        dataMap.put("projectName", railwayBigmodelVO.getProjectName() == null ? "" : railwayBigmodelVO.getProjectName());
        dataMap.put("date", DateUtils.getDate());
        dataMap.put("trainTypes", railwayBigmodelVO.getTrainType() == null ? Collections.singletonList("") : railwayBigmodelVO.getTrainType().split(","));
        dataMap.put("sceneName", railwayBigmodelVO.getSceneName() == null ? "" : railwayBigmodelVO.getSceneName());
        dataMap.put("businessArea", railwayBigmodelVO.getBusinessArea() == null ? "" : railwayBigmodelVO.getBusinessArea());
        dataMap.put("computePower", railwayBigmodelVO.getComputePower() == null ? "" : railwayBigmodelVO.getComputePower().trim());
        dataMap.put("computeResource", railwayBigmodelVO.getComputeResource() == null ? "" : railwayBigmodelVO.getComputeResource());
        dataMap.put("deployNet", railwayBigmodelVO.getDeployNet() == null ? "" : railwayBigmodelVO.getDeployNet());
        dataMap.put("founditionModel", railwayBigmodelVO.getFounditionModel() == null ? "" : railwayBigmodelVO.getFounditionModel());
        dataMap.put("cooperateUnit", railwayBigmodelVO.getCooperateUnit() == null ? "" : railwayBigmodelVO.getCooperateUnit());
        dataMap.put("yuliaoSituation", railwayBigmodelVO.getYuliaoSituation() == null ? "" : railwayBigmodelVO.getYuliaoSituation());
        dataMap.put("name", addSpace(railwayBigmodelVO.getCreateBy(),30));
        dataMap.put("telephone", railwayBigmodelVO.getTelephone() == null ? "" : railwayBigmodelVO.getTelephone());
        dataMap.put("type", railwayBigmodelVO.getType() == null ? DataTypeEnum.YFJSBA.getType() : railwayBigmodelVO.getType());
        dataMap.put("remark",railwayBigmodelVO.getRemark() == null ? "" : railwayBigmodelVO.getRemark());

        return dataMap;
    }

    /**
     * 填写人后面是联系人
     * 填写人与联系人之间需要使用空格填白 实现整行的布局平衡
     * @param name
     * @return
     */
    private String addSpace(String name,int num){
        // 在name为空时 中间总共有num个空格
        if (StringUtils.isNotEmpty(name)){
            num = num - name.length();
            // 每两个中文字符加一个空格
            int countChineseChars = countChineseChars(name);
            num = num - countChineseChars / 2;
        }else {
            name = " ";
        }
        StringBuilder stringBuilder = new StringBuilder(name);
        for (int i = 0; i < num; i++) {
            stringBuilder.append(" ");
        }
        return stringBuilder.toString();
    }

    // 26

    /**
     * 计算字符串中有几个中文字符
     * @param str
     * @return
     */
    public int countChineseChars(String str) {
        if (str == null || str.isEmpty()) {
            return 0;
        }
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]"); // 匹配中文字符的正则表达式
        Matcher matcher = pattern.matcher(str);
        int count = 0;
        while (matcher.find()) { // 查找与模式匹配的子序列
            count++; // 每次找到匹配就增加计数器
        }
        return count;
    }

    @Override
    public int report(RailwayBigmodelVo railwayBigmodelVo) {
        RailwayBigmodel railwayBigmodel = new RailwayBigmodel();
        railwayBigmodel.setReportStatus(ReportStatusEnum.SB.getValue());
        railwayBigmodel.setUpdateTime(new Date());
        railwayBigmodel.setUpdateBy(railwayBigmodelVo.getUser().getName());
        railwayBigmodel.setId(railwayBigmodelVo.getRailwayBigmodel().getId());
        return railwayBigmodelMapper.updateRailwayBigmodel(railwayBigmodel);
    }

    @Override
    public List<RailwayBigmodel> selectRailwayBigmodelListTotal(RailwayBigmodelVo railwayBigmodelVo) {

        railwayBigmodelVo.setSecurityOrg(railwayBigmodelVo.getUser().getQuerySecurityOrg());
        return railwayBigmodelMapper.selectRailwayBigmodelListTotal(railwayBigmodelVo);
    }

    private RailwayBigmodel wordTestDataMap(RailwayBigmodel railwayBigmodel){
        // test data
        RailwayBigmodel railwayBigmodelVO = new RailwayBigmodel();
        railwayBigmodelVO.setProjectName("项目名称");
        railwayBigmodelVO.setComputePower("1");
        railwayBigmodelVO.setBusinessArea("业务领域");
        railwayBigmodelVO.setComputeResource("算力资源");
        railwayBigmodelVO.setTrainType("0,1,2");
        railwayBigmodelVO.setYuliaoSituation("项目名称");
        railwayBigmodelVO.setCooperateUnit("项目名称");
        railwayBigmodelVO.setFounditionModel("基础大模型");
        railwayBigmodelVO.setDeployNet("部署网络");
        railwayBigmodelVO.setCreateBy("填写人");
        railwayBigmodelVO.setTelephone("15533667777");
        railwayBigmodelVO.setSecurityOrgName("建设单位");
        railwayBigmodelVO.setSceneName("应用场景");
        railwayBigmodelVO.setType(DataTypeEnum.YFJSBA.getType().equals(railwayBigmodel.getType()) ? DataTypeEnum.YFJSBA.getType() : DataTypeEnum.SXYYBA.getType());
//        dataMap.put("trainType", railwayBigmodelVO.getTrainType() == null ? "0" : railwayBigmodelVO.getTrainType());
        return railwayBigmodelVO;
    }

}
